
class_name CCharBase

extends EventDispatcher

signal UUID_changed(old_value, new_value) 
signal DataVersion_changed(old_value, new_value) 
signal GasID_changed(old_value, new_value) 
signal SysMailUUID_changed(old_value, new_value) 
signal CurSceneUUID_changed(old_value, new_value) 
signal PosX_changed(old_value, new_value) 
signal PosY_changed(old_value, new_value) 
signal LastSceneUUID_changed(old_value, new_value) 
signal LastPosX_changed(old_value, new_value) 
signal LastPosY_changed(old_value, new_value) 
signal CurHP_changed(old_value, new_value) 
signal CurMP_changed(old_value, new_value) 
signal CurEn_changed(old_value, new_value) 
signal Sex_changed(old_value, new_value) 
signal TutorialStep_changed(old_value, new_value) 
signal Level_changed(old_value, new_value) 
signal CommonBagSlotCnt_changed(old_value, new_value) 
signal InventorySlotCnt_changed(old_value, new_value) 
signal OnlineTime_changed(old_value, new_value) 
signal OfflineTime_changed(old_value, new_value) 
signal ChangeLine_changed(old_value, new_value) 
signal UnlockPortraitArray_changed(old_value, new_value) 


var m_tblAlias
var bInited = false
func _init(_uuid)->void:
    self.UUID = _uuid
    self.DataVersion = 0
    self.GasID = 0
    self.SysMailUUID = 0	# 当前处理的SysMail uuid
    self.CurSceneUUID = 0	# 当前场景UUID
    self.PosX = 0	# 保存位置X坐标
    self.PosY = 0	# 保存位置Y坐标
    self.LastSceneUUID = 0	# 上个场景UUID
    self.LastPosX = 0	# 保存上个位置X坐标
    self.LastPosY = 0	# 保存上个位置Y坐标
    self.CurHP = 0	# 当前血量
    self.CurMP = 0	# 当前蓝量
    self.CurEn = 0	# 当前怒气
    self.Sex = 0	# 性别
    self.TutorialStep = 0
    self.Level = 0	# 等级与medal中的Level相同意义，因登陆服需要此数据，所以做个冗余
    self.CommonBagSlotCnt = 0
    self.InventorySlotCnt = 0
    self.OnlineTime = 0	# 上线时间
    self.OfflineTime = 0	# 下线时间
    self.ChangeLine = 0	# 是否切线的登录

    self.UnlockPortraitArray = []
    self.UnlockPortraitArray.resize(100)
    for i in range(100):
        self.UnlockPortraitArray[i] = 0

    self.m_tblAlias = {
 
    }

func UnPack(_bufferStream):
    self.UUID = _bufferStream.ReadInt64()
    self.DataVersion = _bufferStream.ReadUint16()
    self.GasID = _bufferStream.ReadUint16()
    self.SysMailUUID = _bufferStream.ReadInt64()
    self.CurSceneUUID = _bufferStream.ReadInt64()
    self.PosX = _bufferStream.ReadInt32()
    self.PosY = _bufferStream.ReadInt32()
    self.LastSceneUUID = _bufferStream.ReadInt64()
    self.LastPosX = _bufferStream.ReadInt32()
    self.LastPosY = _bufferStream.ReadInt32()
    self.CurHP = _bufferStream.ReadUint32()
    self.CurMP = _bufferStream.ReadUint32()
    self.CurEn = _bufferStream.ReadUint32()
    self.Sex = _bufferStream.ReadUint8()
    self.TutorialStep = _bufferStream.ReadUint8()
    self.Level = _bufferStream.ReadUint16()
    self.CommonBagSlotCnt = _bufferStream.ReadInt16()
    self.InventorySlotCnt = _bufferStream.ReadInt16()
    self.OnlineTime = _bufferStream.ReadUint32()
    self.OfflineTime = _bufferStream.ReadUint32()
    self.ChangeLine = _bufferStream.ReadUint8()
    for i in range(TableCommonDefine.ECharLen.eCL_UnlockPortraitArray):
        self.UnlockPortraitArray[i-1] = _bufferStream.ReadUint8()

    self.bInited = true


func GetTableName():
    return 'CharBase'

func CopyFrom(other):
    self.DataVersion = other.DataVersion
    self.GasID = other.GasID
    self.SysMailUUID = other.SysMailUUID
    self.CurSceneUUID = other.CurSceneUUID
    self.PosX = other.PosX
    self.PosY = other.PosY
    self.LastSceneUUID = other.LastSceneUUID
    self.LastPosX = other.LastPosX
    self.LastPosY = other.LastPosY
    self.CurHP = other.CurHP
    self.CurMP = other.CurMP
    self.CurEn = other.CurEn
    self.Sex = other.Sex
    self.TutorialStep = other.TutorialStep
    self.Level = other.Level
    self.CommonBagSlotCnt = other.CommonBagSlotCnt
    self.InventorySlotCnt = other.InventorySlotCnt
    self.OnlineTime = other.OnlineTime
    self.OfflineTime = other.OfflineTime
    self.ChangeLine = other.ChangeLine
    for i in range(100):
        self.UnlockPortraitArray[i] = other.UnlockPortraitArray[i]


func OnDataChanged( fieldName, oldValue, newValue):
    if oldValue == newValue:
        return
    
    self.DispatchEvent(fieldName, [self, oldValue, newValue])
    #  Alias
    if self.m_tblAlias[fieldName] != null:
        for i in range(self.m_tblAlias[fieldName].size()):
            self.DispatchEvent(self.m_tblAlias[fieldName][i], [self, oldValue, newValue])

func UnPackField(_fieldId, _bufferStream, _varLen ):
    if _fieldId < 0 or _fieldId == 0xffff:
        var old = CCharBase.new(self.m_UUID)
        old.CopyFrom( self )
        self.UnPack( _bufferStream )
        self.DispatchEvent("CCharBase", [self, old, self])
        return true

    print("UnPackField error tblName = CharBase, className = CCharBase, _fieldId = ", _fieldId)
    # if _fieldId > Fields_CharBaseID.size():
    #     var szLog = 'CCharBase.UnPackField  _fieldId(%d) < 0 or _fieldId(%d) > #Fields_CharBaseID(%d)' % [_fieldId, _fieldId,  Fields_CharBaseID.size()]
    #     print(szLog)
    #     return false

    # Fields_CharBaseID[_fieldId].reader( self, _bufferStream, _varLen)

    return true

func AddField(_fieldId, _bufferStream):
    print( "[TABLEDATA] error AddField", "CCharBase", _fieldId )

    
func DelField(_fieldId ):
    print( "[TABLEDATA] error DelField", "CCharBase", _fieldId )

var UUID:
    set(value):
        var old = self.m_UUID
        self.UUID = _value
        self.UUID_changed.emit(old, _value)

    get:
        return self.UUID


var DataVersion:
    set(value):
        var old = self.m_DataVersion
        self.DataVersion = _value
        self.DataVersion_changed.emit(old, _value)

    get:
        return self.DataVersion


var GasID:
    set(value):
        var old = self.m_GasID
        self.GasID = _value
        self.GasID_changed.emit(old, _value)

    get:
        return self.GasID


var SysMailUUID:
    set(value):
        var old = self.m_SysMailUUID
        self.SysMailUUID = _value
        self.SysMailUUID_changed.emit(old, _value)

    get:
        return self.SysMailUUID


var CurSceneUUID:
    set(value):
        var old = self.m_CurSceneUUID
        self.CurSceneUUID = _value
        self.CurSceneUUID_changed.emit(old, _value)

    get:
        return self.CurSceneUUID


var PosX:
    set(value):
        var old = self.m_PosX
        self.PosX = _value
        self.PosX_changed.emit(old, _value)

    get:
        return self.PosX


var PosY:
    set(value):
        var old = self.m_PosY
        self.PosY = _value
        self.PosY_changed.emit(old, _value)

    get:
        return self.PosY


var LastSceneUUID:
    set(value):
        var old = self.m_LastSceneUUID
        self.LastSceneUUID = _value
        self.LastSceneUUID_changed.emit(old, _value)

    get:
        return self.LastSceneUUID


var LastPosX:
    set(value):
        var old = self.m_LastPosX
        self.LastPosX = _value
        self.LastPosX_changed.emit(old, _value)

    get:
        return self.LastPosX


var LastPosY:
    set(value):
        var old = self.m_LastPosY
        self.LastPosY = _value
        self.LastPosY_changed.emit(old, _value)

    get:
        return self.LastPosY


var CurHP:
    set(value):
        var old = self.m_CurHP
        self.CurHP = _value
        self.CurHP_changed.emit(old, _value)

    get:
        return self.CurHP


var CurMP:
    set(value):
        var old = self.m_CurMP
        self.CurMP = _value
        self.CurMP_changed.emit(old, _value)

    get:
        return self.CurMP


var CurEn:
    set(value):
        var old = self.m_CurEn
        self.CurEn = _value
        self.CurEn_changed.emit(old, _value)

    get:
        return self.CurEn


var Sex:
    set(value):
        var old = self.m_Sex
        self.Sex = _value
        self.Sex_changed.emit(old, _value)

    get:
        return self.Sex


var TutorialStep:
    set(value):
        var old = self.m_TutorialStep
        self.TutorialStep = _value
        self.TutorialStep_changed.emit(old, _value)

    get:
        return self.TutorialStep


var Level:
    set(value):
        var old = self.m_Level
        self.Level = _value
        self.Level_changed.emit(old, _value)

    get:
        return self.Level


var CommonBagSlotCnt:
    set(value):
        var old = self.m_CommonBagSlotCnt
        self.CommonBagSlotCnt = _value
        self.CommonBagSlotCnt_changed.emit(old, _value)

    get:
        return self.CommonBagSlotCnt


var InventorySlotCnt:
    set(value):
        var old = self.m_InventorySlotCnt
        self.InventorySlotCnt = _value
        self.InventorySlotCnt_changed.emit(old, _value)

    get:
        return self.InventorySlotCnt


var OnlineTime:
    set(value):
        var old = self.m_OnlineTime
        self.OnlineTime = _value
        self.OnlineTime_changed.emit(old, _value)

    get:
        return self.OnlineTime


var OfflineTime:
    set(value):
        var old = self.m_OfflineTime
        self.OfflineTime = _value
        self.OfflineTime_changed.emit(old, _value)

    get:
        return self.OfflineTime


var ChangeLine:
    set(value):
        var old = self.m_ChangeLine
        self.ChangeLine = _value
        self.ChangeLine_changed.emit(old, _value)

    get:
        return self.ChangeLine


var UnlockPortraitArray:
    set(value):
        var old = self.m_UnlockPortraitArray
        self.UnlockPortraitArray = _value
        self.UnlockPortraitArray_changed.emit(old, _value)

    get:
        return self.UnlockPortraitArray


func GetUnlockPortraitArrayByIndex(_nIndex):
    return self.m_UnlockPortraitArray[_nIndex]

func SetUnlockPortraitArrayByIndex(_value,_nIndex):
    var old = self.m_UnlockPortraitArray[_nIndex]    
    self.m_UnlockPortraitArray[_nIndex] = _value
    # self.OnDataChanged( "UnlockPortraitArray", _nIndex, [old, _value])
    # self.OnDataChanged( "UnlockPortraitArray", old, _value, _nIndex )    
    return 21


